import Vue from 'vue'

Vue.directive('title', {
  	inserted: function (el, binding) {
   	  	document.title = binding.value
  	}
})


Vue.directive('touch', {
	bind: (el, binding, vnode, oldVnode) => {
		console.log(el, binding, vnode, oldVnode)

		this.touchstart = binding.def.touchstart;
		this.touchend = binding.def.touchend;

		this.touch = {}
	},
	inserted: (el, binding, vnode, oldVnode) => {
		// console.log(el, binding, vnode, oldVnode)
		let type = binding.arg,
			callback = binding.value

		if (typeof callback !== 'function') {
			console.warn('please enter a function')
			return null;
		}

		switch (type) {
			case 'tap':
				binding.def.tap(el,callback);
				break;
			case 'longTap':
				binding.def.longTap(el,callback);
				break;
			case 'doubleTap':
				binding.def.doubleTap(el,callback);
				break;
			case 'swiperLeft':
				binding.def.swiperLeft(el,callback);
				break;
			case 'swiperRight':
				binding.def.swiperRight(el,callback);
				break;
			case 'swiperDown':
				binding.def.swiperDown(el,callback);
				break;
			case 'swiperUp':
				binding.def.swiperUp(el,callback);
				break;
			default:
				throw new Error('参数错误')
		}
	},
	tap: (el, callback) => {
		var start = 0,
			end = 0,
			time = 500;
		el.addEventListener('touchstart', function (e) {
			start = new Date().getTime()
			// let touches = e.changedTouches[0];
		})

		el.addEventListener('touchend', function (e) {
			end = new Date().getTime()
			if (end - start <= time) {
				callback && callback ();
			}else{
				return null;
			}
		})
	},
	doubleTap: (el, callback) => { //双击
		var now = 0,
			last = 0,
			delta = 0,
			time = 250,
			isDoubleBol = false,
			self = this;
		el.addEventListener('touchstart', function (e) {
			now = new Date().getTime();
			delta = now - (last || now);
			if (delta > 0 && delta <= time) {
				isDoubleBol = true;
			}
			last = now;
		})

		el.addEventListener('touchend', function (e) {
			now = new Date().getTime()
			if (now - last <= time) {
				if (isDoubleBol) {
					callback && callback()
					isDoubleBol = false;
					last = 0;
				}
			}
		})
	},
	longTap: (el, callback) => { //长按
		var start = 0,
			end = 0,
			time = 750;
		el.addEventListener('touchstart', function (e) {
			start = new Date().getTime()
		})

		el.addEventListener('touchend', function (e) {
			end = new Date().getTime()
			if (end - start > time) {
				callback && callback ();
			}else{
				return null;
			}
		})
	},
	swiperLeft: (el, callback) => { //左滑
		let self = this;
		el.addEventListener('touchstart', function (e) {
			self.touchstart(e);
		})

		el.addEventListener('touchend', function (e, callback) {
			self.touchend(e);
			if (self.startPageX > self.endPageX && Math.abs(self.startPageX - self.endPageX) > 50) {
				callback &&  callback ();
			}else{
				return null;
			}
		})
	},
	swiperRight: (el, callback) => { //右滑
		let self = this;
		el.addEventListener('touchstart', function (e) {
			self.touchstart(e);
		})

		el.addEventListener('touchend', function (e, callback) {
			self.touchend(e);
			if (self.startPageX < self.endPageX && Math.abs(self.startPageX - self.endPageX) > 50) {
				callback &&  callback ();
			}else{
				return null;
			}
		})
	},

	swiperDown: (el, callback) => { //下滑
		let self = this;
		el.addEventListener('touchstart', function (e) {
			self.touchstart(e);
		})

		el.addEventListener('touchend', function (e, callback) {
			self.touchend(e);
			if (self.startPageY < self.endPageY && Math.abs(self.startPageY - self.endPageY) > 50) {
				callback &&  callback ();
			}else{
				return null;
			}
		})
	},
	swiperUp: (el, callback) => { //上滑
		let self = this;
		el.addEventListener('touchstart', function (e) {
			self.touchstart(e);
		})

		el.addEventListener('touchend', function (e, callback) {
			self.touchend(e);
			if (self.startPageY > self.endPageY && Math.abs(self.startPageY - self.endPageY) > 50) {
				callback &&  callback ();
			}else{
				return null;
			}
		})
	},
	touchstart: (e) => {
		let touches = e.changedTouches[0];
		this.startPageX = touches.pageX;
		this.startPageY = touches.pageY;
	},
	touchend: (e) => {
		let touches = e.changedTouches[0];
		this.endPageX = touches.pageX;
		this.endPageY = touches.pageY;
	}

})